/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package banaanvraag.tools.conversion;

/**
 *
 * @author Luc
 */
public class CoordConversion {
    /**
     * Converts polar coordinates to cartesian coordinates.
     * @param r The radius.
     * @param theta The angle.
     * @return A double[] containing the x at 0 and y at 1.
     */
    public static double[] polarToCartesian(double r, double theta) {
        double[] ret = new double[2];
        ret[0] = r * Math.cos(theta);
        ret[1] = r * Math.sin(theta);
        return ret;
    }
    
    /**
     * Converts cartesian to polar coordinates. Includes 'error correction'.
     * @param x The x-coordinate.
     * @param y The y-coordinate.
     * @return A double containing the r at 0 and the theta at 1.
     */
    public static double[] cartesianToPolar(double x, double y) {
        double errorCorrection = 0;
        if(x < 0) {
            errorCorrection = 180;
        }
        else if(x > 0 && y < 0) {
            errorCorrection = 360;
        }
        
        double[] ret = new double[2];
        ret[0] = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
        ret[1] = Math.atan(x / y) + errorCorrection;
        return ret;
    }
}
